import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
//最小生成树 Prim 模板
public class PrimModel {
    public static Integer n;
    public static Integer m;
    public static final int INF = 0x3f3f3f3;
    public static int[] dist = new int[5005];
    public static int[][] v = new int[5005][505];
    public static boolean[] sti = new boolean[5005];
    public static int[] nums = new int[5005];

    public static void main(String[] args) {
        //System.out.println(Arrays.toString(sti));
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        for (int i = 0; i <= n; i++) {
            Arrays.fill(v[i],INF);
        }
        for (int i = 1; i <= m; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int z = scanner.nextInt();
            v[x][y] = v[y][x] = Math.min(v[x][y],z);
        }
        int ret = prim();
//        System.out.println(ret);
//        for (int i = 2; i <= n; i++) {
//            System.out.print(nums[i]+" ");
//        }
        if(ret == INF){
            System.out.println(-1);
        }else {
            int max = 0;
            for (int i = 1; i <= n; i++) {
                max = Math.max(nums[i],max);
            }
            System.out.println(max);
        }
    }

    public static int prim(){
        int ret = 0;
        Arrays.fill(dist,INF);
        dist[1] = 0;

        for (int i = 0; i < n; i++) {
            int idx = -1;
            for (int j = 1; j <= n; j++) {
                if(!sti[j] && (idx==-1 || dist[idx]>dist[j])){
                    idx = j;
                }
            }
            if(dist[idx] == INF){
                return INF;
            }
            ret += dist[idx];
            sti[idx] = true;
            if(idx!=1){
                nums[idx] = dist[idx];
            }
            for (int j = 1; j <= n; j++) {
                dist[j] = Math.min(dist[j], v[idx][j]);
            }
        }
        return ret;
    }
}






